package org.iweb.html.impl.js.state;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.iweb.html.impl.js.State;

public class HtmlState implements Serializable{

	private Map<String, ElementState> states;
	private List<ElementState> elstates;
	
	public void addState(String id, State state){
		/*
		if(states==null)
			states=new LinkedHashMap<String, ElementState>();
		*/
		if(getStates().get(id)==null){
			getStates().put(id, new ElementState(id));
		}
		//ElementState es=new ElementState();
		//es.
		//getStates().get(id).getStates().add(state);
		getStates().get(id).addState(state);
	}

	public void setStates(Map<String, ElementState> states) {
		this.states = states;
	}

	
	public Map<String, ElementState> getStates() {
		if(states==null)
			states=new LinkedHashMap<String,ElementState>();
		return states;
	}

	public void setElstates(List<ElementState> elstates) {
		this.elstates = elstates;
	}

	public List<ElementState> getElstates() {
		if(elstates==null)
			elstates=new ArrayList<ElementState>();
		elstates.addAll(getStates().values());
		
		ElementState jqscript=null;
		for(ElementState e: elstates){
			if("jqscript".equalsIgnoreCase(e.getId())){
				jqscript=e;
				elstates.remove(e);
				break;
			}
		}
		if(jqscript!=null){
			elstates.add(jqscript);
		}
		
		return elstates;
	}
	
}
